package cn.edu.hqu.cst.android.ljc.ex4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;

import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnBindService,btnUnbindService,btnGetStatus;
    TextView tvServiceStatus;
    MyService.MyServiceBinder serviceBinder;
    String TAG = "MainActivity";

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            serviceBinder = (MyService.MyServiceBinder)iBinder;
            Log.d(TAG,"服务连接上了");//当该Activity与Service连接成功时回调该方法
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.d(TAG,"服务连接结束了");//当该Activity与Service断开连接时回调该方法，解除绑定时并不会调用
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnBindService=findViewById(R.id.btn_main_activity_bind_service);
        btnUnbindService=findViewById(R.id.btn_main_activity_unbind_service);
        btnGetStatus=findViewById(R.id.btn_main_activity_get_status);
        tvServiceStatus=findViewById(R.id.tv_main_activity_service_status);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,MyService.class);
        switch (view.getId()){
            case R.id.btn_main_activity_bind_service:
                //如果 service 尚未绑定就绑定，如果已经绑定则忽略
                if(serviceBinder == null){
                    bindService(intent,conn, Service.BIND_AUTO_CREATE);//绑定Service
                    Toast.makeText(getApplicationContext(),"绑定服务成功",Toast.LENGTH_SHORT).show();
                    btnBindService.setEnabled(false);//绑定之后按钮设置为不可点击
                    btnUnbindService.setEnabled(true);//解绑服务设置为可以点击
                }
                break;
            case R.id.btn_main_activity_get_status:
                //如果 service 已经绑定，获取 service 的 count 计数并显示在截面上。
                if (serviceBinder == null){
                    tvServiceStatus.setText("请先绑定服务");
                    Toast.makeText(getApplicationContext(),"请先绑定服务",Toast.LENGTH_SHORT).show();
                    return;
                }
                int count = serviceBinder.getCount();
                tvServiceStatus.setText("当前服务状态：" + count);
                break;
            case R.id.btn_main_activity_unbind_service:
                //如果 service 已经绑定，则可以解绑，否则忽略
                if (serviceBinder != null){
                    stopService(intent);
                    unbindService(conn);//解绑Service
                    serviceBinder = null;
                    btnBindService.setEnabled(true);//解绑服务后绑定服务设置为可以点击
                    btnUnbindService.setEnabled(false);//解绑服务后解绑按钮不可点击
                    Toast.makeText(getApplicationContext(),"服务解除绑定",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getApplicationContext(),"服务未绑定",Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }


}